
from flask import Blueprint,g,jsonify,current_app
from flask_restful import Api,Resource,reqparse,request
from models.Inquiry_model import db,UserModel,InquiryModel,DoctorModel,CatModel,WenModel
from utils.QiangZhi import login

# 创建蓝图对象：
inqu = Blueprint('inqu',__name__,url_prefix='/inqu')



# 路由收集器：
api = Api(inqu)


# 获取对应的数据：
class InquiryView(Resource):
    def get(self):
#         根据所有的医生获取所有对应的数据：
        doctor_data = DoctorModel.query.all()
        print(doctor_data)

        list_data = []
        for i in doctor_data:
            doc_desc = i.inq
            list_dataz = []
            for j in doc_desc:
                doc = {
                    'id': j.id,
                    'be_good_at':j.be_good_at,
                    'answer_num':j.answer_num,
                    'synth':str(j.synth),
                    'fans_num':j.fans_num,
                    'brief':j.brief,
                    'territory':j.territory,
                    'hospital_img':j.hospital_img,
                    'hospital_name':j.hospital_name,
                    'hospital_addr':j.hospital_addr,
                    'evaluate':j.evaluate,
                }
                list_dataz.append(doc)

            pal = {
                'id':i.id,
                'img':i.img,
                'name':i.name,
                'cong':i.cong_ye,
                'doctor':list_dataz
            }

            list_data.append(pal)

        return jsonify({
            'msg':'获取数据成功',
            'code':200,
            'dat':list_data
        })


api.add_resource(InquiryView,'/doctor')




# 根据id获取医生的单个信息：
class OneView(Resource):
    def get(self,id):

        yi_data = DoctorModel.query.filter(DoctorModel.id == id).first()

        pal = {
            'id':yi_data.id,
            'img':yi_data.img,
            'name':yi_data.name,
            'namel':yi_data.name[0],
            'cong':yi_data.cong_ye,
            # 'answer_num':yi_data.inq.answer_num
        }

        dco = yi_data.inq
        # 用户：
        list_datal = []
        list_data = []
        for i in dco:
            pay = {
                'id':i.id,
                'answer_num':i.answer_num,
                'synth':str(i.synth),
                'fans_num':i.fans_num,
                'brief':i.brief,
                'territory':i.territory,
                'hospital_img':i.hospital_img,
                'hospital_name':i.hospital_name,
                'hospital_addr':i.hospital_addr,
                'evaluate':i.evaluate,
            }
            list_datal.append(pay)

            asd=UserModel.query.filter(UserModel.id == i.user_id).all()
            for j in asd:
                user = {
                    'id':j.id,
                    'username':j.username,
                    'time':str(j.time),
                    'img':j.img
                }
                list_data.append(user)

        return jsonify({
            'msg':'获取单个数据成功',
            'code':200,
            'dat':pal,
            'datz':list_datal,
            'yong':list_data
        })

api.add_resource(OneView,'/one/<int:id>')


# 根据用户获取对应的猫：
class CatlView(Resource):
    def get(self):
        user_id = g.user_id
        print('>>>>>',user_id)
        cat_data =CatModel.query.filter(CatModel.user_id == user_id).first()
        print(cat_data)

        pal = {
            'id':cat_data.id,
            'img':cat_data.img,
            'name':cat_data.name,
            'month':cat_data.month,
            'kind':cat_data.kind,
            'addr':cat_data.addr
        }

        return jsonify({
            'msg':'获取数据成功',
            'code':200,
            'dat':pal
        })

api.add_resource(CatlView,'/cat')



# 添加到问诊表中：
class WenView(Resource):
    @login
    def post(self):
        resp = reqparse.RequestParser()
        resp.add_argument('radio',type=int)
        resp.add_argument('radiol',type=int)
        resp.add_argument('radioz',type=int)
        resp.add_argument('textarea')
        resp.add_argument('yi_id')
        resp.add_argument('cat_id')
        args = resp.parse_args()
        print(args)
        print(">>>>",args['yi_id'])
        print('>>>>>>>>>>>>',args['cat_id'])

        if not all([args['radio'],args['radioz'],args['textarea'],args['yi_id'],args['cat_id']]):
            return jsonify({
                'msg':'数据不齐全！',
                'code':400
            })

#         齐全就添加到数据库中：
        ca = WenModel(
            one = int(args['radio']),
            ther = int(args['radiol']),
            ster = int(args['radioz']),
            desc = args['textarea'],
            doc_id = args['yi_id'],
            cat_id = args['cat_id'],
        )

        db.session.add(ca)
        db.session.commit()

        return jsonify({
            'msg':'问诊成功,请配合！',
            'code':200
        })

api.add_resource(WenView,'/wen')

















